package com.fifth.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Zone;
import com.fifth.common.pojo.entity.ZoneBusiness;
import com.fifth.home.feign.SearchFeign;
import com.fifth.home.mapper.ZoneBusinessMapper;
import com.fifth.home.pojo.vo.ZoneBus;
import com.fifth.home.service.IZoneBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 专区课程关联表 服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-05
 */
@Service
public class ZoneBusinessServiceImpl extends ServiceImpl<ZoneBusinessMapper, ZoneBusiness> implements IZoneBusinessService {
    @Resource
    private ZoneBusinessMapper zoneBusinessMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SearchFeign searchFeign;
    /**
     * 商家管理 根据zoneid 查询初始化页面
     * @param currentPage
     * @param pageSize
     * @param zoneBus
     * @return
     */
    @Override
    public CommonResult managementZoneAndBusiness(Integer currentPage, Integer pageSize, ZoneBus zoneBus) {
        IPage<ZoneBusiness> iPage = new Page<>(currentPage,pageSize);
        QueryWrapper<ZoneBusiness> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",zoneBus.getId());
        wrapper.like(StringUtils.isNotBlank(zoneBus.getSname()),"sname",zoneBus.getSname());
        wrapper.eq(StringUtils.isNotBlank(zoneBus.getIsDisable()),"is_disable",zoneBus.getIsDisable());
        wrapper.orderByAsc("sort");

        IPage<ZoneBusiness> zoneBusinessIPage = zoneBusinessMapper.selectManagementZone(iPage, wrapper);

        return new CommonResult(2000,"查询成功",zoneBusinessIPage);
    }

    /**
     * 查询专区管理所有未上架商店 带分页 带模糊查询   新增弹出层
     * @param currentPage
     * @param pageSize
     * @param zoneBus
     * @return
     */
    @Override
    public CommonResult manageUnstockedZoneAndBusiness(Integer currentPage, Integer pageSize, ZoneBus zoneBus) {
        //System.out.println(zoneBus.toString());
        IPage<ZoneBusiness> iPage = new Page<>(currentPage,pageSize);
        QueryWrapper<ZoneBusiness> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",zoneBus.getId());
        wrapper.like(StringUtils.isNotBlank(zoneBus.getSname()),"sname",zoneBus.getSname());
        wrapper.eq(StringUtils.isNotBlank(zoneBus.getPutaway()),"putaway",zoneBus.getPutaway());
        wrapper.orderByAsc("id");
        //System.out.println(wrapper.getSqlSelect());
        IPage<ZoneBusiness> zoneBusinessIPage = zoneBusinessMapper.selectManagementZoneUnstockedPutaway(iPage, wrapper);
        //System.out.println(zoneBusinessIPage);

        return new CommonResult(2000,"查询成功",zoneBusinessIPage);

    }

    /**
     * 添加  修改未上架的状态
     * @param id
     * @return
     */
    @Override
    public CommonResult addBusinessPutaway(String id) {
        int i = zoneBusinessMapper.updatePutaway(id);
        String head = redisTemplate.opsForValue().get("prefecture");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("prefecture");
        }
        return new CommonResult(2000,"修改成功",null);
    }

    /**
     * 修改上下架状态
     * @param id
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult changeIsDisable(String id, String isDisable) {
        int i =zoneBusinessMapper.updateIsDisable(id,isDisable);
        if(i==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    /**
     * 修改排序
     * @param id
     * @param sort
     * @return
     */
    @Override
    public CommonResult changeSort(String id, Integer sort) {
        ZoneBusiness zoneBusiness = new ZoneBusiness();
        zoneBusiness.setSort(sort);
        QueryWrapper<ZoneBusiness> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int i = zoneBusinessMapper.update(zoneBusiness, wrapper);
        return new CommonResult(2000,"修改排序成功",null);
    }

    /**
     * 删除上架商家
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteZoneBusinessById(String id) {
        int i = zoneBusinessMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",null);
    }

    /**
     * 前端查询商家 每个专区的前四个
     * @return
     */
    @Override
    public CommonResult selectZoneBusiness() {
        //查出每个专区的前四个
        List<ZoneBusiness> list = zoneBusinessMapper.selectByZoneAll();
        return new CommonResult(2000,"查询店铺成功",list);
    }

    /**
     * 前端查询商家
     * @return
     */
    @Override
    public CommonResult selectAllZoneBusiness() {
        List<ZoneBusiness> list = zoneBusinessMapper.selectAll();
        return new CommonResult(2000,"查询店铺成功",list);
    }

    /**
     * 入驻商家成功后，添加中间表zoneBusiness
     * @param zoneId
     * @param bid
     * @return
     */
    @Override
    public CommonResult insert(Integer zoneId, String bid) {
        ZoneBusiness zoneBusiness = new ZoneBusiness();
        zoneBusiness.setZoneId(zoneId.toString());
        zoneBusiness.setPutaway(1);
        zoneBusiness.setSort(1);
        zoneBusiness.setIsDisable(1);
        zoneBusiness.setBid(bid);
        int insert = zoneBusinessMapper.insert(zoneBusiness);
        return new CommonResult(2000,"添加成功",null);
    }

    /**
     *前端搜索框
     * @param pageCurrent
     * @param pageSize
     * @param chindVal
     * @return
     */
    @Override
    public CommonResult selectBusinessByName(Integer pageCurrent, Integer pageSize, String chindVal) {
        IPage<ZoneBusiness> page = new Page<>(pageCurrent,pageSize);
        QueryWrapper<ZoneBusiness> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(chindVal),"sname",chindVal);
        wrapper.eq("status",0);
        IPage<ZoneBusiness> zoneBusinessIPage = zoneBusinessMapper.selectByName(page,wrapper);
        if(zoneBusinessIPage.getTotal()!=0) {
            return new CommonResult(2000, "搜索店铺成功", zoneBusinessIPage);
        }else{
            PageHelper.startPage(pageCurrent,pageSize);
            List<ZoneBusiness> businesByGNmae = searchFeign.findBusinessByGName(chindVal);
            System.out.println(businesByGNmae);
            PageInfo<ZoneBusiness> pageInfo = new PageInfo<>(businesByGNmae);
            Map<String,Object> map = new HashMap<>();
            map.put("records",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            return new CommonResult(2000, "搜索店铺成功", map);
        }
    }

}
